package net.ariel.myideablog.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ariel.myideablog.dao.ICategory;
import net.ariel.myideablog.model.Category;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class CategoryController extends MultiActionController {

	private String viewPage;
	private ICategory iCategory;
	private String errorString;
	private int numberPerPage;
	private int pages;
	private Category category;
	
	public void setICategory(ICategory iCategory) {
		this.iCategory = iCategory;
	}
	
	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}
	
	public void setErrorString(String errorString) {
		this.errorString = errorString;
	}
	
	public void setNumberPerPage(int numberPerPage) {
		this.numberPerPage = numberPerPage;
	}
	
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		// 获得分类总数
		int categoryNumber = iCategory.getCategoryNumber();
		// 计算页数
		if(numberPerPage != 0) {
			pages = categoryNumber/numberPerPage;
			if(categoryNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		List<Category> categoryList = iCategory.getCategoriesByPage(0, numberPerPage);
		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("categories", categoryList);
		mav.addObject("pages", pages);
		mav.addObject("category", category);
		mav.addObject("error", errorString);
		return mav;
	}
	
	public ModelAndView page(HttpServletRequest request, HttpServletResponse response) {
		String page = request.getParameter("page");
		int pageNo = Integer.parseInt(page);
		List<Category> categoryList = iCategory.getCategoriesByPage(pageNo*numberPerPage, (pageNo+1)*numberPerPage);
		
		// 获得分类总数
		int categoryNumber = iCategory.getCategoryNumber();
		// 计算页数
		if(numberPerPage != 0) {
			pages = categoryNumber/numberPerPage;
			if(categoryNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("categories", categoryList);
		mav.addObject("pages", pages);
		mav.addObject("error", errorString);
		return mav;
	}
	/* add category */
	public String add(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("category");
		if(name == null || name.trim().isEmpty()) {
			setErrorString("Empty Category Name");
		}
		else {
			Category category = new Category(name);
			iCategory.saveCategory(category);
			setErrorString(null);
		}
		return "redirect:/admin/category.do";
	}
	
	public String edit(HttpServletRequest request, HttpServletResponse response) {
		String categoryKey = request.getParameter("key");
		if(categoryKey != null) {
			category =  iCategory.getCategory(categoryKey);
		}
		return "redirect:/admin/category.do";
	}
	
	public String update(HttpServletRequest request, HttpServletResponse response) {
		String categoryKey = request.getParameter("key");
		String categoryName = request.getParameter("category");
		if(categoryName == null || categoryName.trim().isEmpty()) {
			setErrorString("Empty Category");
		}
		else {
			iCategory.updateCategory(categoryKey, categoryName);
			category = null;
		}
		return "redirect:/admin/category.do";
	}
	
	/* delete category */
	public String delete(HttpServletRequest request, HttpServletResponse response) {
		String categoryKey = request.getParameter("key");
		iCategory.deleteCategory(categoryKey);
		return "redirect:/admin/category.do";
	}
}
